/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myUI;

import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;

/**
 *
 * @author Administrator
 * @param <T>
 */
public class ArrayListModel<T> extends DefaultListModel<T>{
    
    public ArrayListModel(T[] t){
        for(int i=0;i<t.length;i++)
            this.addElement(t[i]);
    }
    
     public ArrayListModel(){
         super();
     }
     
     public void shiftUp(int index){
         if(index<=0){
             return ;
         }
         List<T> list=new ArrayList<T>();
         for(int i=0;i<getSize();i++){
             list.add(this.getElementAt(i));
         }
        this.clear();
        
        T old=list.get(index);
        T newObject=list.get(index-1);
        
        list.set(index, newObject);
        list.set(index-1, old);
        
        for(T o:list){
            this.addElement(o);
        }
     }
     
     public void shiftDown(int index){
         if(index<0){
             return ;
         }
         if(index==getSize()-1)
             return;
         List<T> list=new ArrayList<T>();
         for(int i=0;i<getSize();i++){
             list.add(this.getElementAt(i));
         }
        this.clear();
        
        T old=list.get(index);
        T newObject=list.get(index+1);
        
        list.set(index, newObject);
        list.set(index+1, old);
        
        for(T o:list){
            this.addElement(o);
        }
     }
     
     public List<T> getAll(){
         List<T> list=new ArrayList<T>();
         for(int i=0;i<getSize();i++){
             list.add(this.getElementAt(i));
         }
         return list;
     }
}
